﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;

public static class TestFindNotSetData {
    static string jsonSaveDir
    {
        get
        {
            const string dirName = "0A4_Config/DefaultTables";
            return Path.Combine(Application.dataPath, dirName);
        }
    }

    private const string jsonDataFileName = "ConstDataJson.json";
    private const string jsonDataTemplateFileName = "ConstDataJson_Template.json";

    [MenuItem("Test/TestFindNotSetJsonData")]
    static void TestFind()
    {
        string jsonFilePath = Path.Combine(jsonSaveDir, jsonDataFileName);
        string jsonTemplateFilePath = Path.Combine(jsonSaveDir, jsonDataTemplateFileName);

        if (!File.Exists(jsonFilePath))
        {
            GameDebug.LogError($"cannot find file:{jsonFilePath}");
            return;
        }
        if (!File.Exists(jsonTemplateFilePath))
        {
            GameDebug.LogError($"cannot find file:{jsonTemplateFilePath}");
            return;
        }

        string jsonDataStr = File.ReadAllText(jsonFilePath);
        string jsonTemplateStr = File.ReadAllText(jsonTemplateFilePath);

        List<string> allJsonKeys = FindJsonKeys(jsonTemplateStr);

        foreach(var key in allJsonKeys)
        {
            if (!jsonDataStr.Contains(key))
            {
                GameDebug.LogError($"cannot find key:{key}");
            }
        }
    }

    static List<string> FindJsonKeys(string str)
    {
        str = str.Trim();

        List<string> res = new List<string>();

        int  i = 0;
        while (str.Contains(":"))
        {
            i++;
            if (i > 1000) break;

            int colonIndex = str.IndexOf(':');

            string preStr = str.Substring(0, colonIndex);
            
            int endQuationIndex = preStr.LastIndexOf('"');

            string leftStr = preStr.Substring(0, endQuationIndex);

            int beginQuationIndex = leftStr.LastIndexOf('"');
            
            string key = leftStr.Substring(beginQuationIndex + 1);

            str = str.Substring(colonIndex + 1);

            res.Add(key);
            //GameDebug.Log($"key :{key}\n  preStr:{preStr} leftStr:{leftStr} str:{str} ");
            
        }
        return res;
    }
	
}
#endif